<?php
if(isset($_POST['action']))
{
    $db = new Database();

    $page = $_POST['page'];
    $cur_page = $page;
    $page -= 1;
    $per_page = 20; // Per page
    $previous_btn = true;
    $next_btn = true;
    $first_btn = true;
    $last_btn = true;
    $start = $page * $per_page;
    $action = $_POST['action'];
    $finaldata = "<table border='0' class='table_accomodation'>
		    <thead>
			<tr>
				<th><center><input type='checkbox' name='selectAll' class='selectAll'/></center></th>
				<th>Date</th>
				<th>Item Code</th>
				<th>Description</th>
				<th>Redeemed Qty</th>
			</tr>
		    </thead>
		    <tbody id='searchDealerName'>";
    if($action=="default")
    {
	$startD = $_POST['startD'];
	$endD = $_POST['endD'];
	$getSummary = $db->query("SELECT * FROM tbredeem WHERE date >= CURDATE() AND date <= CURDATE()
				 LIMIT $start, $per_page","array");
        $count = $db->query("SELECT * FROM tbredeem WHERE date >= CURDATE() AND date <= CURDATE()
				 ","numrows");
	
        $no_of_paginations = ceil($count / $per_page);
    }elseif($action=="Summary"){
	$valueName = $_POST['value'];
	$date = explode(",", $valueName);
	
	
	$getSummary = $db->query("SELECT * FROM tbredeem WHERE date >= '$date[0]' AND date <= '$date[1]'
				   LIMIT $start, $per_page","array");
        $count = $db->query("SELECT * FROM tbredeem WHERE date >= '$date[0]' AND date <= '$date[1]'
				 ","numrows");
	
	$no_of_paginations = ceil($count / $per_page);
    }
    if($count>0){
	foreach($getSummary as $summary){
	    $quan = explode(",",$summary['quantity']);
	    $prodID = explode(",", $summary['product_id']);
	    for($a=0;$a<count($quan);$a++){
		$getProd = $db->query("SELECT * FROM tbproduct WHERE id='$prodID[$a]'","row");
	    $finaldata.='
		<tr>
		    <td style="text-indent:-1px;"><center><input type="checkbox" name="singleSelectAll[]" class="singleSelectAll" rel="'.$summary['id'].'_'.$prodID[$a].'"/></center></td>
		    <td>'.$summary['date'].'</td>
		    <td>'.$getProd['itemCode'].'</td>
		    <td>'.$getProd['product_name'].'</td>
		    <td>'.$quan[$a].'</td>
		</tr>';
	    }
        }
        //pagination
        pagination($cur_page,$page,$per_page,$start,$previous_btn,$next_btn,$first_btn,$last_btn,$no_of_paginations,$finaldata,$count,$action,$valueName);
    }else{
        $finaldata.="<tr><td colspan='5'><center>No Record Found</center></td></tr>";
        echo $finaldata;
    }
}
?>
